<?php
namespace app\overlord\controller;
use app\AdminRest;

use app\overlord\model\Content;
use app\restaurant\model\Store;
use longbingcore\wxcore\WxPay;
use think\App;

use app\overlord\model\Active as model;

use app\overlord\model\Record;

use think\facade\Db;


class AdminAtv extends AdminRest
{

    protected $model;

    protected $record_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->record_model = new Record();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:35
     * @功能说明:列表
     */
    public function atvList(){

        $input = $this->_param;

        $dis[] = ['status','>',-1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['status'])){

            if($input['status']==1){

                $dis[] = ['atv_start_time','>',time()];

            }elseif ($input['status']==2){

                $dis[] = ['atv_start_time','<',time()];

                $dis[] = ['atv_end_time','>',time()];

            }elseif ($input['status']==3){

                $dis[] = ['atv_end_time','<',time()];

            }
        }

        $data = $this->model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                if(!empty($v['user_id'])){

                    $v['user_name'] = lbUserName($v['user_id']);

                }else{

                    $v['user_name'] = '后台发布';
                }

            }
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-09 11:01
     * @功能说明:添加活动
     */
    public function atvAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $info = $this->model->check($input);

        if(!empty($info['code'])){

            $this->errorMsg($info['msg']);
        }

        $res = $this->model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:28
     * @功能说明:详情
     */
    public function atvInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $data['content'] = Db::name('longbing_card_overlord_content_atv')->where(['atv_id'=>$data['id']])->column('content_id');

        $data['content'] = array_values($data['content']);

        $data['store']   = $this->model->selectStore($data['id']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:编辑
     */
    public function atvUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id'],

            'uniacid' => $this->_uniacid
        ];

        $info = $this->model->check($input);

        if(!empty($info['code'])){

            $this->errorMsg($info['msg']);
        }

        $a = $this->model->checkNum($input);

        if(!empty($a['code'])){

            $this->errorMsg($a['msg']);
        }

        $data = $this->model->adminDataUpdate($dis,$input);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2020-11-25 10:02
     * @功能说明:上下架
     */
    public function statusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id'],

        ];

        $res = $this->model->where($dis)->update(['status'=>$input['status']]);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-25 14:26
     * @功能说明:
     */
    public function storeSelect(){

        $store_model = new Store();

        $dis = [

            'uniacid'    => $this->_uniacid,

            'status'     => 1,

            'store_type' => 0
        ];
        //自营
        $data[0] = $store_model->where($dis)->order('top desc')->field('id,title')->select()->toArray();

        $dis['store_type'] = 1;
        //加盟
        $data[1] = $store_model->where($dis)->order('top desc')->field('id,title')->select()->toArray();

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-27 13:55
     * @功能说明:报名列表
     */
    public function recordList(){

        $input = $this->_input;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.atv_id','=',$input['atv_id']];
        //状态
        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }
        //昵称
        if(!empty($input['name'])){

            $dis[] = ['b.nickName','like','%'.$input['name'].'%'];

        }
        //门店
        if(!empty($input['store_id'])){

            $dis[] = ['a.store_id','=',$input['store_id']];

        }

        if(!empty($input['store_title'])){

            $dis[] = ['c.title','like','%'.$input['store_title'].'%'];

        }
        //创建时间
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }

        $data  = $this->record_model->userRecordList($dis,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                if(!empty($v['content'])){

                    foreach ($v['content'] as $vs){

                        $v[$vs['content_id']] = $vs['text'];

                    }
                }

            }

        }

        $where['uniacid'] = $this->_uniacid;

        $where['atv_id']  = $input['atv_id'];
        //所有数量
        $data['all_count']= $this->record_model->where($where)->count();

        $where['status']  = 2;
        //待审核数量
        $data['be_sh_count']= $this->record_model->where($where)->count();

        $where['status']  = 3;
        //通过数量
        $data['pass_count']= $this->record_model->where($where)->count();

        $where['status']  = -1;
        //通过数量
        $data['no_pass_count']= $this->record_model->where($where)->count();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-27 14:49
     * @功能说明:表头
     */

    public function recordTitle(){

        $input = $this->_input;

        $arr = [

            [
                'icon'  => 'nickName',

                'title' => '微信昵称'

            ],
            [
                'icon'  => 'avatarUrl',

                'title' => '微信头像'

            ],
            [
                'icon'  => 'create_time',

                'title' => '报名时间'

            ],
            [
                'icon'  => 'status',

                'title' => '状态'

            ],
            [
                'icon'  => 'store_name',

                'title' => '所属门店'

            ],

        ];

        $title = $this->model->getTitle($input['atv_id']);

        $arr   = array_merge($arr,$title);

        return $this->success($arr);

    }



    /**
     * @author chenniang
     * @DataTime: 2020-11-26 14:27
     * @功能说明:同意
     */
    public function passRecord(){

        $input = $this->_input;

        $update = [

            'sh_time' => time(),

            'text'    => $input['text'],

            'status'  => 3,

            'is_cash' => 1

        ];

        $record = $this->record_model->recordInfo(['id'=>$input['id']]);

        Db::startTrans();

        $res = $this->record_model->dataUpdate(['id'=>$input['id']],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('审核失败');

        }
        //增加储值记录,修改储值
        $res = $this->record_model->storedUpdate($record,$this->_user['admin_id'],$this->_uniacid);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('审核失败');

        }

//        $openid = Db::name('longbing_card_user')->where(['id'=>$record['user_id']])->value('openid');
//        //微信相关模型
//        $wx_pay = new WxPay($this->_uniacid);
//        //微信提现
//        $res    = $wx_pay->crteateMchPay($openid,$record['cash']);
//
//        if($res['result_code']=='SUCCESS'&&$res['return_code']=='SUCCESS'){
//            //if(1==1){
//
//        }else{
//
//            Db::rollback();
//
//            return $this->error(!empty($res['err_code_des'])?$res['err_code_des']:'你还未该权限');
//
//        }

        Db::commit();

        return $this->success(1);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-27 16:48
     * @功能说明:不同意
     */
    public function noPassRecord(){

        $input = $this->_input;

        $update = [

            'sh_time' => time(),

            'text'    => $input['text'],

            'status'  => -1,

            'is_cash' => 0

        ];

        $res = $this->record_model->dataUpdate(['id'=>$input['id']],$update);

        if($res!=1){

            $this->errorMsg('审核失败');
        }

        return $this->success(1);

    }







}
